delphi word
什么是(OLE)自动化? 什么是自动化服务器? 什么是自动化客户端? ( What is (OLE) Automation? What is Automation Server? What is Automation Client? )
Suppose you are developing an HTML editor like HTML Kit. As like any other textual editor your application should contain some kind of spell checking system. Why buy spell checking components or write them from scratch when you can easily use MS Word?
假设您正在开发类似HTML KitHTML编辑器。 与其他任何文本编辑器一样,您的应用程序应包含某种拼写检查系统。 当您可以轻松使用MS Word时,为什么要购买拼写检查组件或从头开始编写呢?
OLE自动化 ( OLE Automation )
one application can control another 一个应用程序可以控制另一台 automation client 自动化客户端 automation server 自动化服务器Automation (also known as OLE Automation) is a feature that programs use to expose their objects to development tools, macro languages, and other programs that support Automation. For example, Microsoft Outlook may expose objects for sending and receiving e-mail, for scheduling, and for contact and task management.
自动化(也称为OLE自动化)是程序用来将其对象公开给支持自动化的开发工具,宏语言和其他程序的功能。 例如,Microsoft Outlook可能会公开用于发送和接收电子邮件,进行计划以及进行联系人和任务管理的对象。
By using Word Automation (server), we can use Delphi (client) to dynamically create a new document, add some text we want to spell check, and then have Word check the spelling. If we keep Microsoft Word minimized, our users might never know! Thanks to Microsoft Word's OLE interface, we can take a side trip from Delphi and look at ways to cheat when developing our version of Notepad editor :)
通过使用Word Automation(服务器),我们可以使用Delphi(客户端)动态创建一个新文档,添加一些我们想要拼写检查的文本,然后让Word检查拼写。 如果我们使Microsoft Word最小化,我们的用户可能永远不知道! 多亏了Microsoft Word的OLE界面,我们可以从Delphi旁走一遍,并研究开发我们的记事本编辑器版本时作弊的方法:)
There's only one glitch ;) Users of the application need to have Word installed. But don't let this stop you.
只有一个小故障;)应用程序的用户需要安装Word。 但是不要让这阻止了你。
Of course, to fully master the use of Automation in your applications, you must have detailed working knowledge of the applications you are integrating - in this case the MS Word.
当然,要完全掌握自动化在您的应用程序中的使用,您必须对要集成的应用程序(在本例中为MS Word)有详细的使用知识。
In order for your "Office" programs to work, the user must own the application that acts like Automation server. In our case MS Word must be installed on the user's machine.
为了使“ Office”程序正常运行,用户必须拥有与自动化服务器类似的应用程序。 在我们的情况下,必须在用户计算机上安装MS Word。
连接到Word:“ Hello Word”早期绑定与后期绑定 ( Connecting to Word: "Hello Word" Early Binding vs. Late Binding )
There are several main steps and three main ways to automate Word from Delphi.
从Delphi自动化Word有几个主要步骤和三种主要方法。
Delphi> = 5-Office XX服务器组件 ( Delphi >= 5 - Office XX Server Components )
TWordApplication TWordApplication TWordDocument TWordDocumentDelphi 3,4-早期绑定 ( Delphi 3,4 - Early Binding )
Type libraries 类型库To use Word's type library in Delphi (version 3 or 4) select the Project | Import Type Library… menu and choose the file msword8.olb located in Microsoft Office's "Office" directory. This will create the file "Word_TLB.pas" which is the object pascal translation of the type library. Include Word_TLB in the uses list of any unit that will be accessing Word properties or methods. Referencing Word methods using the type library is called early binding.
要在Delphi(版本3或版本4)中使用Word的类型库,请选择“项目” |“项目”。 导入类型库…菜单,然后选择位于Microsoft Office的“ Office”目录中的文件msword8.olb。 这将创建文件“ Word_TLB.pas”,它是类型库的对象pascal翻译。 在将要访问Word属性或方法的任何单元的使用列表中包括Word_TLB 。 使用类型库引用Word方法称为早期绑定 。
Delphi 2-后期绑定 ( Delphi 2 - Late Binding )
Late binding 后期装订should be avoided, if possible, since it's much easier and faster to use type libraries - the compiler helps by catching errors in the source. When using late binding Word is declared to be a variable of Variant type. This in particular means than to call methods and access properties you must know what they are.
如果可能的话,应该避免使用它,因为使用类型库要容易得多,而且速度更快-编译器通过捕获源中的错误来提供帮助。 使用后期绑定时,Word被声明为Variant类型的变量。 特别是,这意味着要调用方法和访问属性,您必须知道它们是什么。
静默启动(自动)单词 ( Launching (Automating) Word Silently )
The example in this article will use "server" components provided with Delphi. If you have some earlier version of Delphi I suggest you should use early binding with Word type library.
本文中的示例将使用Delphi提供的“服务器”组件。 如果您有早期版本的Delphi,建议您将早期绑定与Word类型库一起使用。
uses Word_TLB;
...
var
WordApp : _Application;
WordDoc : _Document;
VarFalse : OleVariant;
begin
WordApp := CoApplication.Create;
WordDoc := WordApp.Documents.Add(EmptyParam, EmptyParam) ;
{
spell check code as described
later in this article
}
VarFalse:=False;
WordApp.Quit(VarFalse, EmptyParam, EmptyParam) ;
end;
EmptyParam
空参数
To automate Word with a Variant variable (late binding) use this code:
要使用Variant变量( 后期绑定 )自动执行Word,请使用以下代码:
uses ComObj;
...
var
WordApp, WordDoc: Variant;
begin
WordApp := CreateOleObject('Word.Application') ;
WordDoc := WordApp.Documents.Add;
{
spell check code as described
later in this article
}
WordApp.Quit(False)
end;
“轻松”的方式 ( The "Easy" Way )
these methods and defines several versions with varying numbers of parameters.
这些方法,并定义了多个具有不同数量参数的版本。
拼写检查项目-TWordApplication,TWordDocument ( The Spell Check Project - TWordApplication, TWordDocument )
To build a spell checking project we'll need two forms: one used to edit the text and the other to see the spelling suggestions... but, let's go from the beginning.
要构建拼写检查项目,我们需要两种形式:一种用于编辑文本,另一种用于查看拼写建议...但是,让我们从头开始。
Start Delphi. Create a new project with one blank form (form1, by default). This will be the main form in the spell checking with MS Word project. Add one TMemo (Standard tab) and two TButtons to the form. Add some text to the Memo filling the Lines property. Of course, with some typo errors. Select the Servers tab and add TWordApplication and TWordDocument to the form. Change the name of TWordApplication component from WordApplication1 to WordApp, WordDocument1 to WordDoc.
启动Delphi。 使用一个空白表单(默认为form1)创建一个新项目。 这将是使用MS Word项目进行拼写检查的主要形式。 将一个TMemo (标准选项卡)和两个TButton添加到窗体。 在“备注”中添加一些文本,以填充“线条”属性。 当然,还有一些拼写错误。 选择服务器选项卡,然后将TWordApplication和TWordDocument添加到窗体。 将TWordApplication组件的名称从WordApplication1更改为WordApp,将WordDocument1更改为WordDoc。
TWordApplication,TWordDocument ( TWordApplication, TWordDocument )
The published property ConnectKind is used to control whether we connect to a newly launched Word instance or to an existing instance that is already running. Set ConnectKind to ckRunningInstance.
发布的属性ConnectKind用于控制我们是连接到新启动的Word实例还是连接到已经运行的现有实例。 将ConnectKind设置为ckRunningInstance。
When we open or create a file in Word, we create a Document object. A common task when using automating Word is to specify an area in a document and then do something with it, such as insert text and spell check it. An object that represents a contiguous area in a document is called Range.
当我们在Word中打开或创建文件时,我们将创建一个Document对象。 使用自动Word时,常见的任务是在文档中指定一个区域,然后对其进行处理,例如插入文本并对其进行拼写检查。 表示文档中连续区域的对象称为范围。
拼写检查项目-拼写检查/替换 ( The Spell Check Project - Spell Check / Replace )
The idea is to loop through the text in the Memo and parses it into space delimited words. For each word, we call MS Word to spell check it. Word's Automation model contains the SpellingErrors method that lets you check the spelling of text contained in some Range.
想法是循环浏览备忘录中的文本,并将其解析为以空格分隔的单词。 对于每个单词,我们称其为MS Word进行拼写检查。 Word的自动化模型包含SpellingErrors方法,该方法使您可以检查某些Range中包含的文本的拼写。
Range is defined to contain only the word just parsed out. The SpellingErrors method returns a collection of misspelled words. If this collection contains more that zero words we move on. A call to the GetSpellingSuggestions method, passing in the incorrectly spelled word, fills a SpellingSuggestions collection of suggested replacement words.
范围定义为仅包含刚解析出的单词。 SpellingErrors方法返回拼写错误的单词的集合。 如果此集合包含的单词数超过零,则我们继续。 传入错误拼写的单词的对GetSpellingSuggestions方法的调用将填充SpellingSuggestions集合中的建议替换单词。
We pass this collection to the SpellCheck form. That is the second form in our project.
我们将此集合传递给SpellCheck表单。 那是我们项目中的第二种形式。
To add a new form to a project use File|New Form. Let it have the 'frSpellCheck' name. Add three TBitBtn components on this form. Two EditBox-es and one ListBox. Note the three more Labels. The "Not in dictionary" label is "connected" with the edNID edit box. The edNID simply display the misspelled word. The lbSuggestions list box will list the items in SpellingSuggestions collection. The selected spelling suggestion is placed in the edReplaceWith edit box.
要将新表单添加到项目中,请使用文件|新建表单。 使其具有“ frSpellCheck”名称。 在此表单上添加三个TBitBtn组件。 两个EditBox-es和一个ListBox。 注意另外三个标签。 “不在词典中”标签与edNID编辑框“连接”。 edNID仅显示拼写错误的单词。 lbSuggestions列表框将列出SpellingSuggestions集合中的项目。 所选的拼写建议将放置在edReplaceWith编辑框中。
The three BitButtons are used to Cancel the spell checking, Ignore the current word and to Change the misspelled word with the one in the edReplaceWith edit box. The BitBtn components ModalResult property is used when referring to what the user has clicked. The "Ignore" button has its ModalResult property set to mrIgnore, "Change" to mrOk and "Cancel" to mrAbort.
这三个BitButton用于取消拼写检查,忽略当前单词以及使用edReplaceWith编辑框中的一个来更改拼写错误的单词。 引用用户单击的内容时,将使用BitBtn组件的ModalResult属性。 “忽略”按钮的ModalResult属性设置为mrIgnore,“更改”为mrOk,“取消”为mrAbort。
The frSpellCheck has one Public string variable called sReplacedWord. This variable returns the text in the edReplaceWith when the user presses the "Change" button.
frSpellCheck具有一个名为sReplacedWord的公共字符串变量。 当用户按下“更改”按钮时,此变量将返回edReplaceWith中的文本。
最后:Delphi源代码 ( Finally: Delphi Source Code )
Here goes the parse-and-spell-check procedure:
这是解析和拼写检查过程:
procedure TForm1.btnSpellCheckClick (Sender: TObject) ;
var colSpellErrors : ProofreadingErrors;
colSuggestions : SpellingSuggestions;
j : Integer;
StopLoop : Boolean;
itxtLen, itxtStart : Integer;
varFalse : OleVariant;
begin
WordApp.Connect;
WordDoc.ConnectTo(WordApp.Documents.Add(EmptyParam, EmptyParam)) ;
//main loop
StopLoop:=False;
itxtStart:=0;
Memo.SelStart:=0;
itxtlen:=0;
while not StopLoop do begin
{parse the memo text into words.}
itxtStart := itxtLen + itxtStart;
itxtLen := Pos(' ', Copy(Memo.Text,1+itxtStart, MaxInt)) ;
if itxtLen = 0 then StopLoop := True;
Memo.SelStart := itxtStart;
Memo.SelLength := -1 + itxtLen;
if Memo.SelText = '' then Continue;
WordDoc.Range.Delete(EmptyParam,EmptyParam) ;
WordDoc.Range.Set_Text(Memo.SelText) ;
{call spell check}
colSpellErrors := WordDoc.SpellingErrors;
if colSpellErrors.Count <> 0 then begin
colSuggestions := WordApp.GetSpellingSuggestions (colSpellErrors.Item(1).Get_Text) ;
with frSpellCheck do begin
edNID.text := colSpellErrors.Item(1).Get_Text;
{fill in the list box with suggestions}
lbSuggestions.Items.Clear;
for j:= 1 to colSuggestions.Count do
lbSuggestions.Items.Add(VarToStr(colSuggestions.Item(j))) ;
lbSuggestions.ItemIndex := 0;
lbSuggestionsClick(Sender) ;
ShowModal;
case frSpellCheck.ModalResult of
mrAbort: Break;
mrIgnore: Continue;
mrOK:
if sReplacedWord <> '' then begin
Memo.SelText := sReplacedWord;
itxtLen := Length(sReplacedWord) ;
end;
end;
end;
end;
end;
WordDoc.Disconnect;
varFalse:=False;
WordApp.Quit(varFalse) ;
Memo.SelStart := 0;
Memo.SelLength := 0;
end;
词库? 词库! ( Thesaurus? Thesaurus! )
As a bonus the project has the code to use Word's Thesaurus. Using the thesaurus is quite easier. We don't parse the text, for the selected word the CheckSynonyms method is called. This method displays its own selection dialog. Once a new word is selected, the Word Documents Range contents is used to replace the original word.
作为奖励,该项目具有使用Word同义词库的代码。 使用同义词库非常容易。 我们不解析文本,对于所选单词,将调用CheckSynonyms方法。 此方法显示其自己的选择对话框。 一旦选择了新单词,Word文档范围内容将用于替换原始单词。
翻译自: https://www.thoughtco.com/spell-checking-from-delphi-code-1058149
delphi word